package com.qf.web.controller;

import com.qf.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/goodsImg")
public class GoodsImgServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置编码
        response.setCharacterEncoding("utf-8");
        //获取前端数据
        String pic = request.getParameter("pic");
        if (StringUtil.isEmpty(pic)){
            return;
        }
        String basePath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images" + File.separator + pic);
        File file = new File(basePath);
        if(file.exists()){
            FileInputStream fis = new FileInputStream(file);//输入流
            ServletOutputStream os = response.getOutputStream();//输出流
            byte[]buf=new byte[1024*8];
            int len;
            while ((len=fis.read(buf))!=-1){
                os.write(buf,0,len);
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
